﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kairos.Models;

namespace Kairos.Controllers
{
    public class MappageController : Controller
    {
        private myContext db = new myContext();

        //
        // GET: /Mappage/

        [AccessControl(Roles.Administrator | Roles.Worker)]
        public ActionResult Index(int grapheID)
        {
            Graphe graphe = db.Graphes.Find(grapheID);

            ViewBag.ID = graphe.ID;
            ViewBag.Titre = graphe.Titre;
            ViewBag.Image = graphe.Image;

            IEnumerable<Mappage> mappages = graphe.Mappages;

            return View(mappages);
        }

        //
        // GET: /Mappage/Create

        [AccessControl(Roles.Administrator | Roles.Worker)]
        public ActionResult Create(int grapheID)
        {
            Graphe graphe = db.Graphes.Find(grapheID);

            ViewBag.ID = graphe.ID;
            ViewBag.Titre = graphe.Titre;
            ViewBag.Image = graphe.Image;

            return View();
        }

        //
        // POST: /Mappage/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        [AccessControl(Roles.Administrator | Roles.Worker)]
        public ActionResult Create(Mappage mappage)
        {
            if (ModelState.IsValid)
            {
                db.Mappages.Add(mappage);
                db.SaveChanges();
                return RedirectToAction("Index", new { id = 0, grapheID = mappage.GrapheID });
            }

            Graphe graphe = db.Graphes.Find(mappage.GrapheID);

            ViewBag.ID = graphe.ID;
            ViewBag.Titre = graphe.Titre;
            ViewBag.Image = graphe.Image;

            return View(mappage);
        }

        //
        // GET: /Mappage/Edit/5

        [AccessControl(Roles.Administrator | Roles.Worker)]
        public ActionResult Edit(int id = 0)
        {
            Mappage mappage = db.Mappages.Find(id);
            if (mappage == null)
            {
                return HttpNotFound();
            }

            Graphe graphe = db.Graphes.Find(mappage.GrapheID);

            ViewBag.ID = graphe.ID;
            ViewBag.Titre = graphe.Titre;
            ViewBag.Image = graphe.Image;

            return View(mappage);
        }

        //
        // POST: /Mappage/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        [AccessControl(Roles.Administrator | Roles.Worker)]
        public ActionResult Edit(Mappage mappage)
        {
            if (ModelState.IsValid)
            {
                db.Entry(mappage).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index", new { id = 0, grapheID = mappage.GrapheID });
            }

            Graphe graphe = db.Graphes.Find(mappage.GrapheID);

            ViewBag.ID = graphe.ID;
            ViewBag.Titre = graphe.Titre;
            ViewBag.Image = graphe.Image;

            return View(mappage);
        }

        //
        // POST: /Mappage/Delete/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        [AccessControl(Roles.Administrator | Roles.Worker)]
        public ActionResult Delete(int id)
        {
            Mappage mappage = db.Mappages.Find(id);
            db.Mappages.Remove(mappage);
            db.SaveChanges();
            return RedirectToAction("Index", new { id = 0, grapheID = mappage.GrapheID });
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}